﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sinacor.Infra.Tools.ServiceRegistration.Business
{
    public class Method
    {
        #region Attributes

        private Data.Method _resourceAccess;
        private Business.Type _businessType;
        private object locker = new object();

        #endregion

        #region Constructor

        public Method()
        {
            this._resourceAccess = new Data.Method();
            this._businessType = new Business.Type();
        }

        #endregion

        #region Methods

        public List<Entity.Method> SelectMethod(Entity.Method entity)
        {
            return _resourceAccess.SelectMethod(entity, true);
        }

        public Entity.Method PersistMethod(Entity.Method entity)
        {
            Entity.Method method = new Entity.Method() { MethodName = entity.MethodName, Service = entity.Service };
            List<Entity.Method> methods = null;
            Business.Parameter parameter = new Business.Parameter();

            methods = _resourceAccess.SelectMethod(method, false);

            if (methods.Count == 0)
            {
                method = InsertMethod(entity);
            }
            else
            {
                method = methods[0];

                if (method.StatusApproval == Entity.StatusApproval.AguardandoAprovacao && !string.IsNullOrEmpty(entity.Description))
                {
                    method.Description = entity.Description;
                    method.RelatedTables = entity.RelatedTables;
                }
                                
                method.ReturnType = entity.ReturnType;
                method.IsReturnCollection = entity.IsReturnCollection;
                method.DateChange = entity.DateChange;
                method.DataOrigin = entity.DataOrigin;
                UpdateMethod(method);
            }

            return method;
        }

        public Entity.Method InsertMethod(Entity.Method entity)
        {
            if (!entity.ReturnType.TypeId.HasValue)
            {
                if (!string.IsNullOrEmpty(entity.ReturnType.FullTypeName))
                    entity.ReturnType = _businessType.PersistType(entity.ReturnType);
                else
                {
                    entity.ReturnType.FullTypeName = "System.Void";
                    entity.ReturnType = _businessType.PersistType(entity.ReturnType);
                }
            }

            entity.MethodId = this.GetNextMethodId();
            return _resourceAccess.InsertMethod(entity);
        }

        public void UpdateMethod(Entity.Method entity)
        {
            if (!entity.ReturnType.TypeId.HasValue)
            {
                if (string.IsNullOrEmpty(entity.ReturnType.FullTypeName))
                    entity.ReturnType.FullTypeName = "System.Void";
                
                entity.ReturnType = _businessType.PersistType(entity.ReturnType);
            }

            _resourceAccess.UpdateMethod(entity);
        }

        public void DeleteMethod(Entity.Method entity)
        {
            _resourceAccess.DeleteMethod(entity);
        }

        public int GetNextMethodId()
        {
            int nextId = 0;

            lock (locker)
            {
                nextId = _resourceAccess.GetNextMethodId();
            }

            return nextId;
        }

        #endregion
    }
}
